package logic

import (
	"baidu/baidurpc/baidurpc"
	"baidu/baidurpc/internal/svc"
	"baidu/modil/cache"
	"context"
	"fmt"
	"math/rand"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type UploadLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadLogic {
	return &UploadLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UploadLogic) Upload(in *baidurpc.UploadRequest) (*baidurpc.UploadResponse, error) {
	// todo: add your logic here and delete this line
	get := cache.Rdb.Get(l.ctx, "sendSmsNum"+in.Mobile)
	if get.Val() >= "1" {
		return nil, fmt.Errorf("您当前一分钟之内已经发送过一次,请稍后再发")
	}

	code := rand.Intn(9000) + 1000
	//sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if *sms.Body.Code != "OK" {
	//	return nil, fmt.Errorf(*sms.Body.Message)
	//}

	cache.Rdb.Set(l.ctx, "sendSms"+in.Mobile, code, time.Minute*2)

	cache.Rdb.Incr(l.ctx, "sendSmsNum"+in.Mobile)
	cache.Rdb.Expire(l.ctx, "sendSmsNum"+in.Mobile, time.Minute*1)
	return &baidurpc.UploadResponse{}, nil
}
